﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;

using dotnetCHARTING;
using ChartExtents;

public partial class main1211 : System.Web.UI.Page
{
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
        UpdateTowerTable();
     

    }
    protected void UpdateTowerTable()
    {
        //更新grid
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        DataTable dataTable = towerDAOs.getMetricsDataTable(Session["LineName"].ToString(), Session["TowerName"].ToString());
        if (dataTable.Rows.Count> 0)
        {
            
            GridView1.DataSource = dataTable;
            GridView1.DataBind();
        }
        //Drawing();
        
        if (!IsPostBack)
        {
            //更新dropList
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                String s = dataTable.Columns[i].ColumnName;
                if (!s.StartsWith("红外报警"))
                {
                    s = s.Substring(0, s.IndexOf("("));
                    DropDownList1.Items.Add(s);
                }
            }

            //更新台账信息
            Accounting acc = towerDAOs.getAccoutingInfoDataTable(Session["LineName"].ToString(), Session["TowerName"].ToString());
            Label_LineNum.Text = acc.LineName;
            Label_TowerNum.Text = acc.TowerName;
            Label_TowerType.Text = acc.TowerType;
            Label_Install.Text = acc.Description;
            Label_Span.Text = acc.Span;
           // DataTable datatable1 = towerDAOs.getMetricsHistoryDataTable(Session["LineName"].ToString(), Session["TowerName"].ToString(), DateTime.Now, DateTime.Now.AddDays(1));
        }
       
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=ExcelFile.csv");
        Response.Charset = "gb2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.ContentType = "application/text";
        StringBuilder sb = new StringBuilder();
        DataTable dt = new DataTable();
        dt = (DataTable)GridView1.DataSource;
        for (int k = 0; k < dt.Columns.Count; k++)
        {
            sb.Append(dt.Columns[k].ColumnName + ',');
        }
        sb.Append(Environment.NewLine);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int k = 0; k < dt.Columns.Count; k++)
            {
                //add separator 
                sb.Append(dt.Rows[i][k].ToString() + ',');
            }
            //append new line 
            sb.Append(Environment.NewLine);
        }
        Response.Output.Write(sb.ToString());
        Response.Flush();
        Response.End(); 

    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=TextFile.txt");
        Response.ContentType = "application/vnd.ms-word";
        Response.Charset = "";
        StringBuilder sb = new StringBuilder();
        DataTable dt = new DataTable();
        dt = (DataTable)GridView1.DataSource;
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            sb.AppendFormat("{0,-10}", dt.Columns[i].ColumnName);


        }
        sb.Append(Environment.NewLine);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                sb.AppendFormat("{0,-10}", dt.Rows[i][j].ToString());

            }
            sb.Append(Environment.NewLine);
        }
        Response.Write(sb.ToString());
        Response.End();

    }
  
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        Drawing();
        
        
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        
    }
    private void Drawing()
    {
        Charting c = new Charting();
        //Chart[] chart = { this.Chart1, this.Chart2, this.Chart3, this.Chart4, this.Chart5, this.Chart6, this.Chart7 };
        c.Title = "";
        c.XTitle = "时间";
        c.YTitle = DropDownList1.SelectedValue;
        c.PicHight = 280;
        c.PicWidth = 550;
        //c.SeriesName = "合计";//仅对于DataTable类型做数据源时，此属性有效
        c.PhaysicalImagePath = "images";//统计图片存放的文件夹名称，缺少对应的文件夹生成不了统计图片
        c.FileName = "Statistics";
        c.Type = SeriesType.Spline;
        c.Use3D = false;
        c.DataSource = GetDataSource();
        c.CreateStatisticPic(this.Chart1);

    }
    private SeriesCollection GetDataSource()
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        SeriesCollection SC = new SeriesCollection();
        DataSet ds;

        ds = towerDAOs.getSpineB(Session["TowerName"].ToString(), this.DropDownList1.SelectedValue, DateTime.Now.Date, DateTime.Now.Date.AddDays(1));
        
        //else if (j == 1)
        //{
        //    ds = towerDAOs.getSpineB(dropTower.SelectedValue, RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        //}
        //else if (j == 2)
        //{
        //    ds = towerDAOs.getSpineB(dropTower.SelectedValue, "风速", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        //}
        //else if (j == 3)
        //{
        //    ds = towerDAOs.getSpineB(dropTower.SelectedValue, "风向", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        //}
        //else if (j == 4)
        //{
        //    ds = towerDAOs.getSpineB(dropTower.SelectedValue, "气压", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        //}
        //else if (j == 5)
        //{
        //    ds = towerDAOs.getSpineB(dropTower.SelectedValue, "雨量", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        //}
        //else
        //{
        //    ds = towerDAOs.getSpineB(dropTower.SelectedValue, "日照", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        //}

        int i = ds.Tables[0].Rows.Count;
        if (i != 0)
        {
            Series s = new Series();
    
                s.Name = DropDownList1.SelectedValue;
                for (int b = 0; b < 10; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart1.Visible = true;
           
            //else if (j == 1)
            //{
            //    s.Name = "湿度";
            //    for (int b = 0; b < 10; b++)
            //    {
            //        Element e = new Element();
            //        e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
            //        e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
            //        s.Elements.Add(e);
            //    }
            //    SC.Add(s);
            //    this.Chart2.Visible = true;
            //}
            //else if (j == 2)
            //{
            //    s.Name = "风速";
            //    for (int b = 0; b < 10; b++)
            //    {
            //        Element e = new Element();
            //        e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
            //        e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
            //        s.Elements.Add(e);
            //    }
            //    SC.Add(s);
            //    this.Chart3.Visible = true;
            //}
            //else if (j == 3)
            //{
            //    s.Name = "风向";
            //    for (int b = 0; b < 10; b++)
            //    {
            //        Element e = new Element();
            //        e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
            //        e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
            //        s.Elements.Add(e);
            //    }
            //    SC.Add(s);
            //    this.Chart4.Visible = true;
            //}
            //else if (j == 4)
            //{
            //    s.Name = "气压";
            //    for (int b = 0; b < 10; b++)
            //    {
            //        Element e = new Element();
            //        e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
            //        e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
            //        s.Elements.Add(e);
            //    }
            //    SC.Add(s);
            //    this.Chart5.Visible = true;
            //}
            //else if (j == 5)
            //{
            //    s.Name = "雨量";
            //    for (int b = 0; b < 10; b++)
            //    {
            //        Element e = new Element();
            //        e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
            //        e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
            //        s.Elements.Add(e);
            //    }
            //    SC.Add(s);
            //    this.Chart6.Visible = true;
            //}
            //else if (j == 6)
            //{
            //    s.Name = "日照强度";
            //    for (int b = 0; b < 10; b++)
            //    {
            //        Element e = new Element();
            //        e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
            //        e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
            //        s.Elements.Add(e);
            //    }
            //    SC.Add(s);
            //    this.Chart7.Visible = true;
            //}
        }
        return SC;
    }
}
